<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ProductphotosController extends AppController {

    var $name = 'Productphoto';
    var $components = array(
        'Session', 'Alias', 'Upload', 'Resize','Crop', 'Watermark'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array('Productphoto');

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - thư viện ảnh');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $product_id = array_key_exists('product_id', $this->params['named']) ? $this->params['named']['product_id'] : 0;


        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        $conditions = array(
            'Productphoto.product_id' => $product_id,
        );
        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Productphoto.ordering' => 'DESC'
            ),
        );
        $data = $this->paginate('Productphoto');

        $this->set('total', $this->params['paging']['Productphoto']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
        $this->set('product_id', $this->params['named']['product_id']);
    }

    function remove() {
        $conditions = array(
            "{$this->Productphoto->alias}.{$this->Productphoto->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        //delete images small
        $data = $this->Productphoto->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Productphoto.folder_path', 'Productphoto.images')
                        )
        );
        foreach ($data AS $folder_path => $image_file) {
            //delete it
            if (is_file(Configure::read('product_gallery_thumb_absolute_path') . $folder_path . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('product_gallery_thumb_absolute_path') . $folder_path . $image_file);
            }
        }
        //delete images small (300x350)
        $data = $this->Productphoto->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Productphoto.folder_path', 'Productphoto.image_small')
                        )
        );
        foreach ($data AS $folder_path => $image_file) {
            //delete it
            if (is_file(Configure::read('product_gallery_thumb_absolute_path') . $folder_path . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('product_gallery_thumb_absolute_path') . $folder_path . $image_file);
            }
        }

        if ($this->Productphoto->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d hình ảnh", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index', 'product_id' => $this->params['named']['product_id']));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Productphoto->id = $params['cid'][0];
        } else {
            $this->Productphoto->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        if (empty($this->data)) {
            $fields = array(
                'title', 'images', 'id', 'product_id', 'folder_path', 'folder_url', 'image_small'
            );
            $this->data = $this->Productphoto->read($fields);
            $check_thumbnail_exist = is_file(Configure::read('product_gallery_thumb_absolute_path') . $this->data['Productphoto']['folder_url'] . $this->data['Productphoto']['images']);
            $this->set('check_thumbnail_exist', $check_thumbnail_exist);

            $check_small_image_exist = is_file(Configure::read('product_gallery_thumb_absolute_path') . $this->data['Productphoto']['folder_url'] . $this->data['Productphoto']['image_small']);
            $this->set('check_small_image_exist', $check_small_image_exist);

            $this->set('image_url', Configure::read('product_gallery_thumb_url_path') . $this->data['Productphoto']['folder_url'] . $this->data['Productphoto']['images']);
            $this->set('small_image_url', Configure::read('product_gallery_thumb_url_path') . $this->data['Productphoto']['folder_url'] . $this->data['Productphoto']['image_small']);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm  thư viện ảnh');
        $this->set('product_id', $this->params['named']['product_id']);
    }

    function crop() {
        $form = $this->params['form'];
        $data = $this->data['Productphoto'];
        $row = $this->Productphoto->findById($this->data['Productphoto']['id']);

        $img_300_350_filename = Configure::read('product_gallery_thumb_absolute_path') . $row['Productphoto']['folder_path'] . $form['img_300_350_filename'];
        if ($this->Crop->loadImage($img_300_350_filename)) {
            $this->Crop->cropToDimensions($form['img_300_350_sx'], $form['img_300_350_sy'], $form['img_300_350_ex'], $form['img_300_350_ey']);
            $this->Crop->saveImage($img_300_350_filename);
            $this->Crop->flushImages();
        }
        $this->Session->setFlash('đã crop ảnh thành công', 'msg', array(), 'msg');
        $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $data['product_id']));
    }

    function save() {
        $data = $this->data['Productphoto'];
        $row = $this->Productphoto->findById($this->data['Productphoto']['id']);

        if (!empty($row['Productphoto']['folder_path'])) {
            $folder_path = $row['Productphoto']['folder_path'];
            $path_file_need_delete = Configure::read('product_gallery_thumb_absolute_path') . $row['Productphoto']['folder_path'];
        } else {
            $folder_path = $this->Upload->createFolder();
            $path_file_need_delete = Configure::read('product_gallery_thumb_absolute_path') . $folder_path;
        }
        //for upload thumbnail
        $this->Upload->setUploadpath($path_file_need_delete);
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));

        $data['folder_path'] = $folder_path;
        $data['folder_url'] = str_replace('\\', '/', $folder_path);

        //upload large image
        if ($this->data['Productphoto']['images']['name'] != '') {
            if ($this->Upload->validate($this->data['Productphoto']['images'])) {
                $data['images'] = $this->Upload->upload($this->data['Productphoto']['images']);
                $image_fullsize_path    =   $path_file_need_delete . $data['images'];
                
                $this->Resize->run_resize($path_file_need_delete . $data['images'], 500, 500);
                $this->Watermark->watermark($image_fullsize_path);
                //delete it
                if (is_file($path_file_need_delete . $row['Productphoto']['images']) && $row['Productphoto']['images'] != 'no_image.gif') {
                    $this->Upload->delete($path_file_need_delete . $row['Productphoto']['images']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $data['product_id']));
                return;
            }
        } else {
            unset($data['images']);
            //delete image
            if (!empty($this->data['Productphoto']['delete_image'])) {
                if (is_file($path_file_need_delete . $row['Productphoto']['images']) && $row['Productphoto']['images'] != 'no_image.gif') {
                    $this->Upload->delete($path_file_need_delete . $row['Productphoto']['images']);
                    $data['images'] = 'no_image.gif';
                }
            }
        }
        //end large image
        //upload small image (300x350
        if ($this->data['Productphoto']['image_small']['name'] != '') {
            if ($this->Upload->validate($this->data['Productphoto']['image_small'])) {
                $data['image_small'] = $this->Upload->upload($this->data['Productphoto']['image_small']);
                $image_smallsize_path   =   $path_file_need_delete . $data['image_small'];
                
                $this->Resize->run_resize($path_file_need_delete . $data['image_small'], 300, 350);
                $this->Watermark->watermark($image_smallsize_path);
                //delete it
                if (is_file($path_file_need_delete . $row['Productphoto']['image_small']) && $row['Productphoto']['image_small'] != 'no_image.gif') {
                    $this->Upload->delete($path_file_need_delete . $row['Productphoto']['image_small']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $data['product_id']));
                return;
            }
        } else {
            unset($data['image_small']);
            //delete image
            if (!empty($this->data['Productphoto']['delete_image_small'])) {
                if (is_file($path_file_need_delete . $row['Productphoto']['image_small']) && $row['Productphoto']['image_small'] != 'no_image.gif') {
                    $this->Upload->delete($path_file_need_delete . $row['Productphoto']['image_small']);
                    $data['image_small'] = 'no_image.gif';
                }
            }
        }
        
        $data['alias'] = $this->Alias->alias($data['title']);
        if (empty($data['id'])) {
            //add watermark
            $data['ordering'] = $this->Productphoto->nextOrder(array('Productphoto.product_id' => $data['product_id']));
        }
        if ($this->Productphoto->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Productphoto->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $data['product_id']));
    }

    function cancel() {
        $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $this->data['Productphoto']['product_id']));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Productphoto->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Productphoto->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $this->params['named']['product_id']));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Productphoto->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Productphoto->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'product_photos', 'action' => 'index', 'product_id' => $this->params['named']['product_id']));
    }

    function saveorder() {
        $ordering = $this->params['form']['order'];

        $product_id = isset($this->params['named']['product_id']) ? $this->params['named']['product_id'] : 0;

        foreach ($ordering AS $id => $order) {
            $this->Productphoto->set('id', $id);
            $this->Productphoto->saveField('ordering', $order);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('action' => 'index', 'product_id' => $product_id));
    }

}

?>
